package be.novelfaces.component.numpaddecseparator;

import static org.easymock.EasyMock.expect;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ResponseWriter;

import org.apache.myfaces.shared_impl.renderkit.html.HtmlResponseWriterImpl;
import org.jboss.test.faces.mock.MockFacesEnvironment;
import org.junit.After;
import org.junit.Before;

public class AbstractMockFacesTestCase {
	private MockFacesEnvironment environment;
	private UIComponent componentMock;
	private UIViewRoot uiViewRootMock;
	private ResponseWriter responseWriter;
	private Map<String, Object> attributesMap;
	private StringWriter stringWriter;
	
	@Before
	public void setUp() throws Exception {
		// create mock environment
		environment = MockFacesEnvironment.createEnvironment();
		
		stringWriter = new StringWriter();
		responseWriter = new HtmlResponseWriterImpl(stringWriter, "text/html", "UTF-8");

		// create mock component
		componentMock = environment.createMock(UIComponentBase.class);
		uiViewRootMock = environment.createMock(UIViewRoot.class);

		expect(environment.getFacesContext().getResponseWriter())
				.andReturn(responseWriter);

		// set up attributes map
		attributesMap = new HashMap<String, Object>();
		expect(componentMock.getAttributes()).andStubReturn(attributesMap);
	}

	@After
	public void tearDown() throws Exception {
		// do verification for mocked objects
		environment.verify();

		// do clean up
		environment.release();
		environment = null;

		componentMock = null;
		attributesMap = null;
		responseWriter.close();
	}

	public MockFacesEnvironment getEnvironment() {
		return environment;
	}

	public UIComponent getComponentMock() {
		return componentMock;
	}

	public Map<String, Object> getAttributesMap() {
		return attributesMap;
	}
	
	public UIViewRoot getUiViewRootMock() {
		return uiViewRootMock;
	}
	
	public StringWriter getStringWriter() {
		return stringWriter;
	}
	
}